﻿using QuanLyThuVien.DTO;
using QuanLyThuVien.Supporter;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng CT_PhieuNhap
    //----------------------------------------------------------------------
    public class DALCT_PhieuNhap
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALCT_PhieuNhap()
        { }

        //----------------------------------------------------------------------
        //Desc: Chuyển dòng dữ liệu thành CT_PhieuNhap
        //----------------------------------------------------------------------
        public DTOCT_PhieuNhap ConvertToCT_PhieuNhap(DataRow row)
        {
            DTOCT_PhieuNhap ctpn = new DTOCT_PhieuNhap();
            ctpn.MaPhieuNhap = row["MaPhieuNhap"].ToString();
            ctpn.Sach.MaSach = row["MaSach"].ToString();
            try
            {
                ctpn.SoLuong = Int32.Parse(row["SoLuong"].ToString());
            }
            catch { }
            try
            {
                ctpn.TriGia = Double.Parse(row["TriGia"].ToString());
            }
            catch { }
            try
            {
                ctpn.ThanhTien = Double.Parse(row["ThanhTien"].ToString());
            }
            catch { }

            return ctpn;
        }

        //----------------------------------------------------------------------
        //Desc: Lấy danh sách CT_PhieuNhap có trong PhieuNhap
        //----------------------------------------------------------------------
        public List<DTOCT_PhieuNhap> LayDSCT_PhieuNhap(string maphieunhap)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spCT_PhieuNhap_LayDSCT_PhieuNhap";
            SqlParameter sqlprMaPhieuNhap = new SqlParameter("@MaPhieuNhap", SqlDbType.VarChar, 15)
            { Value = maphieunhap };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaPhieuNhap);
            if (data == null)
                return null;
            List<DTOCT_PhieuNhap> list = new List<DTOCT_PhieuNhap>();
            foreach (DataRow row in data.Rows)
            {
                DTOCT_PhieuNhap ctpn = ConvertToCT_PhieuNhap(row);
                if (ctpn != null)
                    list.Add(ctpn);
            }
            return list;
        }

        //----------------------------------------------------------------------
        //Desc: Cập nhật CT_PhieuNhap
        //----------------------------------------------------------------------
        public bool CapNhat(DTOCT_PhieuNhap ctpn)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spCT_PhieuNhap_CapNhatCTPN";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprMaPhieuNhap = new SqlParameter(@"MaPhieuNhap", SqlDbType.VarChar, 15) { Value = ctpn.MaPhieuNhap };
            SqlParameter sqlprMaSach = new SqlParameter("@MaSach", SqlDbType.VarChar, 15) { Value = ctpn.Sach.MaSach };
            SqlParameter sqlprSoLuong = new SqlParameter("@SoLuong", SqlDbType.Int) { Value = ctpn.SoLuong };
            SqlParameter sqlprTriGia = new SqlParameter("@TriGia", SqlDbType.Money) { Value = ctpn.TriGia };
            SqlParameter sqlprThanhTien = new SqlParameter("@ThanhTien", SqlDbType.Money) { Value = ctpn.ThanhTien };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprMaPhieuNhap,
                sqlprMaSach, sqlprSoLuong, sqlprTriGia, sqlprThanhTien);
        }
    }
}
